package org.wit.ecommerce.service.impl;

import java.util.List;
import java.util.Map;

import org.wit.ecommerce.dao.DiscountDao;
import org.wit.ecommerce.domain.Discount;
import org.wit.ecommerce.dto.Navigator;
import org.wit.ecommerce.service.IDiscountService;
import org.wit.ecommerce.util.PageNavigateCalculator;
/**
 * @spring.bean id="discountService" parent="baseTransactionProxy" extends="true" transactionProxy="true"
 * @spring.property name="discountDao" ref="discountDao"
 * @author Justin
 *
 */
public class DiscountServiceImpl implements IDiscountService {
	private DiscountDao discountDao;
	@Override
	public void createDiscount(Discount discount) {
		discountDao.save(discount);
	}

	@Override
	public void updateDiscount(Discount discount) {
		discountDao.save(discount);
	}

	@Override
	public void deleteDiscount(Long id) {
		discountDao.delete(id);
	}

	@Override
	public Navigator<Discount> getDiscountsByPage(int page, Map<String, ?> keywords) {
		PageNavigateCalculator pnc = new PageNavigateCalculator();
		pnc.setCurrentPage(page);
		pnc.setTotalRowsAmount(discountDao.getTotalCount(keywords));
		int start = pnc.getCurrentPageRows();
		int limit = pnc.getPageSize();
		
		List<Discount> discounts = discountDao.findByPage(start, limit, keywords);
		Navigator<Discount> navigator = new Navigator<Discount>();
		navigator.setData(discounts);
		navigator.setNext(pnc.HasNext());
		navigator.setPageSize(pnc.getPageSize());
		navigator.setPrevious(pnc.HasPrevious());
		navigator.setTotalPages(pnc.getTotalPages());
		navigator.setTotalRowsAmount(pnc.getTotalRowsAmount());
		return navigator;
	}

	public void setDiscountDao(DiscountDao discountDao) {
		this.discountDao = discountDao;
	}

	@Override
	public Discount getDiscountById(Long id) {
		return discountDao.get(id);
	}

}
